package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.ScenicTag;
import cn.wolfcode.luowowo.article.domain.Ticket;
import cn.wolfcode.luowowo.article.domain.TicketDetail;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicTagService;
import cn.wolfcode.luowowo.article.service.ITicketDetailService;
import cn.wolfcode.luowowo.article.service.ITicketService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import lombok.extern.java.Log;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("homePage")
@Controller
public class TicketController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private ITicketService ticketService;
    @Reference
    private IScenicTagService scenicTagService;
    @Reference
    private ITicketDetailService ticketDetailService;
    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;

    @RequestMapping("")
    public String index(Long destId, Model model){
        //查询好评率前三的4个景点票务信息
        List<Ticket> top4 = ticketService.listTop4(destId);
        model.addAttribute("top4",top4);

        //目的地前2个
        List<Destination> list = destinationService.listTop7ByParentId(destId);
        model.addAttribute("dlist",list);

        //默认查询第一个目的地的标签、第一个标签的票价信息
        Long destinationId = list.get(0).getId();
        List<ScenicTag> tags = scenicTagService.listTag7(destinationId);
        model.addAttribute("tags",tags);
        //票价信息
        List<Ticket> tickets = ticketService.listByDestIdAndTagId(destinationId,tags.get(0).getId());
        model.addAttribute("tickets",tickets);

        return "ticket/index";
    }

    @RequestMapping("reloadSubAndProd")
    public String themes(Long ajaxDestId, Model model){
        //某个目的地下的主题, 前8个
        List<ScenicTag> tags = scenicTagService.listTag7(ajaxDestId);
        model.addAttribute("tags",tags);

        //查询某个目的地下的,第一个主题的景点票价
        List<Ticket> tickets = ticketService.listByDestIdAndTagId(ajaxDestId,tags.get(0).getId());
        model.addAttribute("tickets",tickets);

        return "ticket/indexTpl";
    }

    @RequestMapping("reloadProd")
    public String reloadProd(Model model, Long ajaxDestId, Long ajaxSubjectId){
        //查询某个目的地下某个主题的票务信息
        List<Ticket> tickets = ticketService.listByDestIdAndTagId(ajaxDestId,ajaxSubjectId);
        model.addAttribute("tickets",tickets);
        return "ticket/tagTpl";
    }

    @RequestMapping("detail")
    public String detail(Long tid, Model model, @LoginUser UserInfo userInfo){
        model.addAttribute("userInfo",userInfo);

        Ticket ticket = ticketService.getById(tid);
        model.addAttribute("ticket",ticket);

        TicketDetail ticketDetail = ticketDetailService.getByTicketId(tid);
        model.addAttribute("ticketDetail",ticketDetail);

        return "ticket/detail";
    }

    @RequestMapping("addOrder")
    public String addOrder(Model model, Long tid, Long ticId , @LoginUser UserInfo userInfo){
        if (userInfo == null) {
            return "/login.html";
        }
        model.addAttribute("userInfo",userInfo);

        Ticket ticket = ticketService.getById(ticId);
        model.addAttribute("ticket",ticket);

        TicketDetail ticketDetail = ticketDetailService.getByTicketId(ticId);
        model.addAttribute("ticketDetail",ticketDetail);

        return "ticket/addOrder";
    }

    @RequestMapping("order")
    @ResponseBody
    @RequiredLogin
    public Object order(String phone, String verifyCode){
        return new AjaxResult();
    }
}
